package com.xjprojectc.uitl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * ������ת�����ַ�
	 * 
	 * @param date
	 * @return
	 */
	public static String dateFormat(Date date) {
		return df.format(date);
	}

	/**
	 * ���ַ�ת��������
	 * 
	 * @param date
	 * @return
	 */
	public static Date dateFormat(String date) {
		try {
			return df.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * ����birthday����ȷ��(���Ϊ�����ʾ�û�û������,�򷵻�true;������ڸ�ʽʱû���쳣,�򷵻�true,�����false)
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isDate(String date) {
		if (date == null || "".equals(date)) {
			return true;// ���Ϊ�����ʾ�û�û������,�򷵻�true
		} else if (DateUtil.dateFormat(date) != null) {
			if (isFormat(date) && idGtNow(date)) {
				return true;// ������ڸ�ʽʱû���쳣,�����ڸ�ʽΪ(yyyy-mm-dd)�����������û�д��ڵ�ǰϵͳ�����򷵻�true
			}
			return false;// ������ڸ�ʽʱû���쳣,�������'yyyy-mm-dd'��ʽ���򷵻�false
		} else {
			return false;
		}
	}

	/**
	 * ���ڸ�ʽ���ж�(yyyy-mm-dd)
	 * ͨ��'-'�����ڵ��ַ���зָ�,�õ�����ĳ������Ϊ3,�ҵ�1������(��)�ĳ���Ϊ4,�ҵ�2������(��)�ĳ���Ϊ2,�ҵ�3������(��)�ĳ���Ϊ2
	 * ,�򷵻�true;�����false
	 * 
	 * @param date
	 * @return
	 */
	private static boolean isFormat(String date) {
		String[] dates = date.split("-");
		int yearLength = dates[0].length();// ��ĳ���
		int monthLength = dates[1].length();// �µĳ���
		int dayLength = dates[2].length();// �յĳ���
		if (dates.length == 3 && monthLength == 2 && yearLength == 4
				&& dayLength == 2) {
			return true;
		}
		return false;
	}

	/**
	 * �ж�����������Ƿ���ڵ�ǰ����,�����򷵻�false,���򷵻�true
	 * 
	 * @param date
	 * @return
	 */
	private static boolean idGtNow(String date) {
		long now = System.currentTimeMillis();
		long input = 0;
		try {
			input = df.parse(date).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (input > now) {
			return false;// �����򷵻�false
		}
		return true;
	}

	public static String getStringDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String res = "";
		try {
			return sdf.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	public static int timeDifference(String time1, String time2) {
		try {
			SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化时间
			int result = Integer.parseInt(String.valueOf((d.parse(time1)
					.getTime() - d.parse(time2).getTime()) / 1000 / 60));// 当前时间减去测试时间
			// 这个的除以1000得到秒，相应的60000得到分，3600000得到小时
			return result;
		} catch (Exception e) {
			return 0;
		}

	}
	
	public static int timeDifference(String time1, String time2,String type) {
		try {
			SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化时间
			int result = 0;
			type = type.trim().toLowerCase();
			// 这个的除以1000得到秒，相应的60000得到分，3600000得到小时
			if(type.equals("s")){
				result = Integer.parseInt(String.valueOf((d.parse(time1)
						.getTime() - d.parse(time2).getTime()) / 1000));
			}else if(type.equals("m")){
				result = Integer.parseInt(String.valueOf((d.parse(time1)
						.getTime() - d.parse(time2).getTime()) / 1000 / 60));
			}else if(type.equals("h")){
				result = Integer.parseInt(String.valueOf((d.parse(time1)
						.getTime() - d.parse(time2).getTime()) / 1000 / 60 /60));
			}
			return result;
		} catch (Exception e) {
			return 0;
		}

	}

	public static String getNearTime(String time, String type, int nearNum) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String res = "";
			Calendar cal = null;
			Date date = null;
			;
			if (!"".equals(time)) {
				date = sdf.parse(time.trim());
				cal = Calendar.getInstance();
				cal.setTime(date);

			} else {
				cal = Calendar.getInstance();
			}
			if (!"".equals(type)) {
				type = type.trim().toLowerCase();
				if (type.equals("m")) {
					cal.add(Calendar.MINUTE, nearNum);
				} else if (type.equals("h")) {
					cal.add(Calendar.HOUR, nearNum);
				}
			}
			res = sdf.format(cal.getTime());
			return res;

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		System.out.println("a".toLowerCase());
		System.out.println(getNearTime("2013-11-12 12:00:00", "m", 65));
	}
}
